package com.base.common.view.adapter;

import android.graphics.Color;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.base.common.utils.LogUtil;
import com.base.common.view.adapter.ada.BaseRVAdapter;

import java.util.Collections;

public class MyItemTouchHelper extends ItemTouchHelper {


    public MyItemTouchHelper() {
        super(new MyCallBack());
    }


    public static class MyCallBack extends ItemTouchHelper.Callback {

        private int fromPositionOld = -1, toPositionOld = -1;

        @Override
        public int getMovementFlags(RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
            int dragFrlg = 0;
            if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
                dragFrlg = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
            } else if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {
                dragFrlg = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
            }
            return makeMovementFlags(dragFrlg, 0);
        }

        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            //滑动事件  下面注释的代码，滑动后数据和条目错乱，被舍弃
//            Collections.swap(datas,viewHolder.getAdapterPosition(),target.getAdapterPosition());
//            ap.notifyItemMoved(viewHolder.getAdapterPosition(),target.getAdapterPosition());

            RecyclerView.Adapter adapter = recyclerView.getAdapter();

            if (adapter instanceof BaseRVAdapter) {
                BaseRVAdapter baseRVAdapter = (BaseRVAdapter) adapter;
                //得到当拖拽的viewHolder的Position
                int fromPosition = viewHolder.getAdapterPosition();
                //拿到当前拖拽到的item的viewHolder
                int toPosition = target.getAdapterPosition();
                if (fromPosition == fromPositionOld && toPosition == toPositionOld) {
                    return true;
                } else {
                    fromPositionOld = fromPosition;
                    toPositionOld = toPosition;
                }

                baseRVAdapter.move(fromPosition, toPosition);
                Collections.swap(baseRVAdapter.getDatas(), viewHolder.getAdapterPosition(), target.getAdapterPosition());
                LogUtil.d("  " + fromPosition + "  " + toPosition);
//                baseRVAdapter.moveNext(fromPosition, toPosition);
            }

            return true;
        }


        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
            int position = viewHolder.getAdapterPosition();
            if (direction == ItemTouchHelper.END) {
//                adapter.getAllData().remove(position);
//                adapter.notifyItemRemoved(position);
            }
        }

        @Override
        public boolean isLongPressDragEnabled() {
            return true;
        }

        /**
         * 长按选中Item的时候开始调用
         * 长按高亮
         *
         * @param viewHolder
         * @param actionState
         */
        @Override
        public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
            if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
                viewHolder.itemView.setBackgroundColor(Color.RED);
                //获取系统震动服务//震动70毫秒
//                Vibrator vib = (Vibrator) UIUtils.getContext().getSystemService(Service.VIBRATOR_SERVICE);
//                if (vib != null) vib.vibrate(70);
            }
            super.onSelectedChanged(viewHolder, actionState);
        }

        /**
         * 手指松开的时候还原高亮
         *
         * @param recyclerView
         * @param viewHolder
         */
        @Override
        public void clearView(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
            super.clearView(recyclerView, viewHolder);

            viewHolder.itemView.setBackgroundColor(0);

//            if (recyclerView.isComputingLayout()) {
//                recyclerView.post(new Runnable() {
//                    @Override
//                    public void run() {
//                        viewHolder.itemView.setBackgroundColor(0);
//                        if (recyclerView.getAdapter() != null)
//                            recyclerView.getAdapter().notifyDataSetChanged();  //完成拖动后刷新适配器，这样拖动后删除就不会错乱
//                    }
//                });
//            } else {
//                viewHolder.itemView.setBackgroundColor(0);
//                if (recyclerView.getAdapter() != null)
//                    recyclerView.getAdapter().notifyDataSetChanged();  //完成拖动后刷新适配器，这样拖动后删除就不会错乱
//            }
        }

    }
}
